<template>
	<header id="nav">
		<div class="i_maxW i_headOne">
			<a :href="BASE_URL"  class="i_logo box">
				<img :src="BASE_URL + logo_url" alt="logo">
			</a>
			<div class="meunP">
				<template v-for="item  in selectNav" :index="item.name">
					<router-link 
						:key="item.id"
						v-if="item.name == 'personal'" 
						to="/personal/index"
						:class="{'Indexactive':$route.path.split('/')[1] === item.path, 'activePath': item.name === 'class'}">
						{{item.txtName}}
					</router-link>
					<router-link  v-else-if=" item.name !== 'personal' && item.children.length !== 0 "
						:key="item.id"
						:to="'/'+item.path+'/'+item.children[0].path+'/'+item.children[0].children[0].path"
						:class="{'Indexactive':$route.path.split('/')[1] === item.path, 'activePath': item.name === isActivePath}">
						{{item.txtName}}
					</router-link>
				</template>
			</div>
			<!-- 已登录 -->
			<div class="i_landing">
				<div class="i_user">
					<div class="i_userName clear">
						<span class="i_uPortrait fl">
							<img :src="BASE_URL + $store.getters.userInfo.avatar_path">
						</span>
						<div class="i_username fl">
							<div>
								<strong>{{$store.getters.userInfo.name}}</strong>
							</div>
							<div>
								<strong>{{$store.getters.userInfo.check_role_name}}</strong>
							</div>
						</div>
					</div>
				</div>
				<!--  -->
				<div class="userinfoH">
					<div class="userinfoH_1">
						<a @click="switchingIdentity = true" v-if=" $store.getters.userInfo.rolebaseid.length >= 2">
							<i class="ipm icon_qiehuanshenfen"></i>
							切换身份
						</a>
						<a @click="pathURL('admin')"
						v-if='$store.getters.userInfo.check_rolebaseid <= 3'>
							<i class="ipm icon_guanlizhongxin"></i>
							管理中心
						</a>
						<router-link to="/message/notice/index" v-if="messageName">
							<i class="ipm icon_youjian"></i>
							我的消息
						</router-link>
						<a  @click="pathURL('settings')">
							<i class="ipm icon_shezhi"></i>
							账号设置
						</a>
						<a @click="pathURL('home')">
							<img src="../../assets/images/home.png" class="home_icon">
							返回首页
						</a>
					</div>

					<p @click="loginOut()">安全退出</p>
				</div>
			</div>
		</div>
		<el-dialog
			title="切换身份"
			:visible.sync="switchingIdentity"
			width="400px"
			custom-class="switchingIdentit"
			:close-on-click-modal="false">
			<div class="identity">

				<div class="box"  v-for="(item,index) in $store.getters.userInfo.rolebaseid" :key="item.id">
					<label>
						<img src="../../../public/images/id_1.png" v-if="item <= 3">
						<img src="../../../public/images/id_2.png" v-else-if="item == 5">
						<img src="../../../public/images/id_3.png" v-else-if="item == 6">
						<img src="../../../public/images/id_1.png" v-else>
						<el-radio v-model="identity" :label="item"></el-radio>
						<div class="">
							<i class="el-icon-check"></i>
						</div>
						
					</label>
					<p>{{$store.getters.userInfo.rolename[index]}}</p>
				</div>
			</div>
			<div slot="footer" class="buttonCSS">
				<el-button type="primary" plain @click="switchingIdentity=false">取消</el-button>
				<el-button type="primary" @click="switchIdentity">确定</el-button>
			</div>
		</el-dialog>

	</header>
</template>

<script>


	export default {
		name: 'top',
		data(){
			return {
				userName: '',
				classID: '',
				manage_url: '',
				switchingIdentity: false,
	        	selectNav: deep_clone(  this.$router.options.routes[1].children ),
	        	loading: {
	        	 	txt: '',
	        	 	loading: ''
	        	 },
	        	 isActivePath: '',
	        	 user_id: this.$store.getters.userInfo.id,
	        	 logo_url: '',
	        	 identity: this.$store.getters.userInfo.check_rolebaseid
			}
		},
		watch: {

		},
		computed: {
			messageName(){
				let msg = this.selectNav.filter(item=>item.path === "message")
				return msg.length > 0 ? true : false
			}
		},
		mounted() {
			let manage_url = sessionStorage.getItem( 'manage_url' )
			let redirect_params = sessionStorage.getItem( 'redirect_params' )
			this.manage_url = manage_url + redirect_params
		},
		created(){
			this.getUserClass()
			this.getLogo()
		},
		methods: {
			/**
			 * 切换身份
			 * @qiu_m
			 * @DateTime 2019-10-19T09:10:38+0800
			 * @param    {[type]}                 type [description]
			 * @return   {[type]}                      [description]
			 */
			switchIdentity(){

				let success = (res=>{
					this.switchingIdentity = false;
					this.$message.success("已切换身份");
					this.checkLogin()
					let host = sessionStorage.getItem( 'host' )
					let redirectParams = sessionStorage.getItem( 'redirect_params' )
					let link = `${this.BASE_URL}/personal${redirectParams}&host=${host}`
					window.location.href = link

				});
				let params = { check_role: this.identity};

				this.$ajax.put('api/login/check_login_section', params, {func: {success: success}})
			},
			//查询登录情况
			checkLogin(){

				let success = (res=>{})
				let params = {}
				this.$ajax.get('api/login/login', {params, func:{success: success}})
			},
			pathURL(type){

				let host = sessionStorage.getItem( 'host' )
				let redirectParams = sessionStorage.getItem( 'redirect_params' )
				let CheckSchoolAppCode =  sessionStorage.getItem( 'CheckSchoolAppCode' )
				let link = ''

				switch(type){
					case 'admin':
						link = `${this.BASE_URL}/admin${redirectParams}&host=${host}&CheckSchoolAppCode=${CheckSchoolAppCode}`
						window.open(link)
					break;
					case 'settings':
						link = `${this.BASE_URL}${redirectParams}&host=${host}&routerUrl=setting`
						window.open(link)
					break;
					case 'home':
						link = `${this.BASE_URL}${redirectParams}&host=${host}`
						window.location.href = link
					break;

				}
				
			},
			getLogo(){
				let success = (res=>{
					this.logo_url = res.data[0].t_logo;
				})

				this.$ajax({
					url: 'api/system/get_logo',
					method: 'get',
					params: {},
					func: {success: success}
				})
			},
			loginOut(){
        		  this.loading.txt = '正在退出个人中心...'
        		  this.loading.full = false
        		  // window.location.href = sessionStorage.getItem('host_addr')+'/cas/logout'
        		if (this.$store.getters.userInfo.logout_url !== undefined) {

	        			this.$message.success('退出成功...')

	        			setTimeout(() => {
	        				window.location.href = this.$store.getters.userInfo.logout_url;
							
	        				sessionStorage.clear();
	        				localStorage.clear();
	        			}, 1000)
        		  		
        		} else {

					let success = ( data => {
						this.$message.success('退出成功...')
						
						//2后跳回首页
						setTimeout(() => {
							// window.location.href = sessionStorage.getItem('host_addr')+'/cas/logout'
							window.location.href = sessionStorage.getItem('host_addr');
							sessionStorage.clear();
                            localStorage.clear();
						}, 1000)
					});

					let fail = ( data => {
						this.$message.error(data.msg)
						this.loading.txt = data.msg;
						this.loading.full = true
						//2后跳回首页
						setTimeout(() => {
							this.loading.status =  false;
							this.loading.full = false
						}, 1000)

					});
					this.$ajax.delete('api/login/new_login',{params:{},func:{success:success,fail:fail}})
        		  } 
			     
        	},

        	//判断用户是否归属班级，没有班级就隐藏班级空间
        	getUserClass(){
        		let success = (res =>{
        			let data = res.data;
        			if(data.length === 0){
        				this.selectNav.forEach(item =>{
        					if (item.name === "class") {
        						this.isActivePath = item.name
        					}
        				})
        			}else{
        				sessionStorage.setItem('parent_child_class_id',data[0].id)
        				sessionStorage.setItem('classId',data[0].id)

        			}
        			
        		});

        		let fail = (res=>{})

        		this.$ajax({
        			url: 'api/org/class_userid',
        			method: 'get',
        			params: {
        				user_id: this.$g.user_id,
        				style: 1,
        				data_type: 0
        			},
        			func: {
        				success: success,
        				fail: fail
        			}
        		})
        	},	
		},
	}
</script>